<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/20
 * Time: 17:26
 */
namespace App\Http\Controllers\Api\v1\Push;

use App\Http\Controllers\Api\BaseController;
use App\Models\Api\User;
use Illuminate\Http\Request;

/**
 * @title 推送绑定
 * @desc 用户登录或者开启通知权限后进行，注册id绑定
 * @url
 * @version 1.1.2
 * Class Bind
 * @readme /doc/md/push/base.md
 */
class BindController extends BaseController
{
    protected $restActionList = ['save'];

    /**
     * @title 推送设备绑定
     * @method POST
     * @desc /api/push/bind
     * @readme
     */
    public function save(Request $request)
    {
        // 用户id
        $uid = $request->uid;
        // 注册id
        $regid = $request->input('regid');

        if ($uid && $regid) {
            try
            {
                (new User())->updateRegid(['uid' => $uid, 'regid' => $regid]);

            } catch (\Exception $e) {
                return show(config('code.error'), $e->getMessage(), [], 500);
            }
        }

        return show(200, '提交成功', []);
    }

    /**
     * 推送所有人
     */
    public function push_all()
    {
        $alert = "我来试试全部用户推送，赶快去查看吧！";
        $msg   = [
            "extras" => [
                "version" => '1.2.0',
                "target"  => "link",
                "address" => "http://www.goddessxzns.com",
            ],
        ];

        $result = $this->notifyAllUser($alert, $msg, config('jpush.app_key'), config('jpush.master_secret'));
        dd($result);
    }

    /**
     * 推送给特定用户
     */
    public function push_special()
    {
        $alert  = '测试推送';
        $action = 'index';
        $id     = '0';
        $params = array(
            "extras" => [
                "target" => "local",
                "link"   => $action . "?id=" . $id,
            ],
        );
        $regid = ['1507bfd3f6c1a2e2791','121c83f7612587edf41','121c83f7606531ae5ea','18071adc020e85824ca','140fe1da9fafdf3e71f','160a3797c900010ecd9']; // android , ios

        $result = $this->sendNotifySpecial($regid, $alert, $params, config('jpush.app_key'), config('jpush.master_secret'));
        dd($result);
    }

    /**
     * 向所有设备推送消息-广播
     * @param $alert
     * @param $message
     * @param $app_key
     * @param $master_secret
     * @return mixed
     */
    private function notifyAllUser($alert, $message, $app_key, $master_secret)
    {
        $client = new \JPush\Client($app_key, $master_secret);

        $result = $client->push()
            ->addAllAudience() // 推送所有
            ->setPlatform('all')
            ->iosNotification($alert, $message)
            ->androidNotification($alert, $message)
            ->send();

        return $this->json_array($result);
    }

    /**
     * 将数据先转换成json,然后转成array
     */
    private function json_array($result)
    {
        $result_json = json_encode($result);
        return json_decode($result_json, true);
    }

    /**
     * 向特定设备推送消息
     * @param $regid string 设备注册id
     * @param $alert
     * @param $message
     * @param $app_key
     * @param $master_secret
     * @return mixed
     */
    private function sendNotifySpecial($regid, $alert, $param, $app_key, $master_secret)
    {
        $client = new \JPush\Client($app_key, $master_secret);

        $androidParam = [
            "builder_id" => 1,
            "style"      => 0,
            "priority"   => 1,
            "extras"     => $param,
        ];

        $iosParam = array(
            'badge' => '+1',
            'sound' => 'sub.caf',
            'extras' => array(
                "target" => "local",
                'link' => 'astrolable'
            ));
        $platform = ['ios', 'android'];
        $options  = [
            'apns_production' => true,
        ];

        $result = $client->push()
            ->setPlatform($platform)
            ->addRegistrationId($regid)
            ->iosNotification($alert, $iosParam)
            ->androidNotification($alert, $androidParam)
           // ->options($options)
            ->send();
        return $this->json_array($result);
    }

}
